package cn.zxcoder.config;

import cn.zxcoder.entity.Statistics.Count;
import cn.zxcoder.service.ICountNumService;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.MDC;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.time.LocalDate;
import java.util.Calendar;
import java.util.Date;
import java.util.concurrent.TimeUnit;

//@Slf4j
@Component
@Api(tags = "定时任务，用于每个月将点击量存入数据库")
public class MyScheduled {

    @Autowired
    ICountNumService iCountNumServiceIml;

    @Scheduled(cron = "0 0 12 1 * ?")
    public void task() {
        Calendar instance = Calendar.getInstance();
        RedisTemplate<String, Integer> redisTemplate = new RedisTemplate<>();
        Integer count = redisTemplate.opsForValue().getAndDelete("product:count");
        Count count1 = new Count(instance.get(Calendar.MONTH)+1,count);
        iCountNumServiceIml.save(count1);

        //重新设置数量
        redisTemplate.opsForValue().set("product:count",  0, 32, TimeUnit.DAYS);
    }
}
